<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>console.interactive</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="console-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="http://code.google.com/p/console/">console</a></th>
          </tr></table></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        <a href="console-module.html">Package&nbsp;console</a> ::
        Module&nbsp;interactive
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
        <tr><td align="right"><span class="options">[<a href="javascript:void(0);" class="privatelink"
    onclick="toggle_private();">hide&nbsp;private</a>]</span></td></tr>
        <tr><td align="right"><span class="options"
            >[<a href="frames.html" target="_top">frames</a
            >]&nbsp;|&nbsp;<a href="console.interactive-module.html"
            target="_top">no&nbsp;frames</a>]</span></td></tr>
      </table>
    </td>
  </tr>
</table>
<!-- ==================== MODULE DESCRIPTION ==================== -->
<h1 class="epydoc">Module interactive</h1><p class="nomargin-top"><span class="codelink"><a href="console.interactive-pysrc.html">source&nbsp;code</a></span></p>
<p>Framework for creating command line programs with an interactive 
  prompt.</p>
  <h1 class="heading">Quick Start</h1>
    <p>This module contains the base classes for creating interactive 
    console programs. To get started quickly, simply create commands by 
    extending <a href="console.interactive.InteractiveCommand-class.html" 
    class="link">InteractiveCommand</a> and overriding the methods exposed 
    by the interface. Then instantiate an <a 
    href="console.interactive.InteractiveShell-class.html" 
    class="link">InteractiveShell</a> with your customized prompt and 
    banner. Then add instances of your <a 
    href="console.interactive.InteractiveCommand-class.html" 
    class="link">InteractiveCommand</a> via <a 
    href="console.interactive.InteractiveShell-class.html#add_command" 
    class="link">InteractiveShell.add_command</a>. Finally use <a 
    href="console.interactive.InteractiveShell-class.html#run" 
    class="link">InteractiveShell.run</a> to run the shell with your 
    commands, and presto-- you have a complete interactive console 
    program!</p>
    <h2 class="heading">Sample Code</h2>
      <p>The testland quickstart example:</p>
<pre class="literalblock">
   if __name__ == '__main__':

       class TestCommand(InteractiveCommand):
           
           def get_name(self):
               return 'test'
   
           def get_options(self):
               return { 'opt1': None,
                        'opt2=': 'default',
                        'required=': None}
   
           def handle_command(self, opts, args):
               print 'Test command running with options:'
   
               for k,v in opts.items( ):
                   print '%s =&gt; %s' % (str(k), str(v))
               
               print 'Args is: %s' % args
               
           def get_short_description(self):
               return 'I am a test command hear me roar'
   
           def get_help_message(self):
               return 'test --opt1=value [--opt2=value2]'
   
           
       tc = TestCommand( )
   
       shell = InteractiveShell( banner='Welcome to TestLand',
                              prompt=' TEST &gt;&gt;&gt; ')
   
       shell.add_command(tc)
   
       shell.run( )
</pre>
      <p>As you can see, it's simple to get up and running quickly with an 
      interactive console program!</p>
  <h1 class="heading">Extending <a href="console.interactive.InteractiveCommand-class.html" class="link">InteractiveCommand</a></h1>
    <p><a href="console.interactive.InteractiveCommand-class.html" 
    class="link">InteractiveCommand</a> is the base class for every command
    that you will add to your interactive console program. It exposes an 
    interface by forcing you to ovverride its methods by raising a 
    <code>NotImplementedError</code> if the child class relies on the super
    method.</p>
    <p>There are no properites specifically associated with <a 
    href="console.interactive.InteractiveCommand-class.html" 
    class="link">InteractiveCommand</a>, but the name, description, and 
    help text must be implemented via their associated methods. You can add
    any number of helper methods to your children class, or any number of 
    properties to make the command function as you wish. However, note that
    the <a 
    href="console.interactive.InteractiveCommand-class.html#handle_command"
    class="link">InteractiveCommand.handle_command</a> method is entry 
    point to the command.</p>
  <h1 class="heading">Extending <a href="console.interactive.InteractiveShell-class.html" class="link">InteractiveShell</a></h1>
    <p><a href="console.interactive.InteractiveShell-class.html" 
    class="link">InteractiveShell</a> comes with built-in help and exit 
    commands, which are internal classes. While there is no real reason to 
    extend the shell from scratch, note that you cannot have access to the 
    <a href="console.interactive.InteractiveShell._ExitCommand-class.html" 
    class="link" 
    onclick="show_private();">InteractiveShell._ExitCommand</a> or the <a 
    href="console.interactive.InteractiveShell._HelpCommand-class.html" 
    class="link" 
    onclick="show_private();">InteractiveShell._HelpCommand</a> unless you 
    override them in your own <a 
    href="console.interactive.InteractiveShell-class.html" 
    class="link">InteractiveShell</a>.</p>
    <p>Larger console programs should not rely on using an <code>if 
    __name__ == '__main__'</code> block to execute code, but rather should 
    extend <a href="console.interactive.InteractiveShell-class.html" 
    class="link">InteractiveShell</a> via adding customized 
    {InteractiveCommand}s as internal classes, then ensuring that they are 
    added to the shell in <a 
    href="console.interactive.InteractiveShell-class.html#__init__" 
    class="link">InteractiveShell.__init__</a>. This way, all that has to 
    be put in the main method is <a 
    href="console.interactive.InteractiveShell-class.html#run" 
    class="link">InteractiveShell.run</a>.</p>
  <h1 class="heading">Module Class Hierarchy</h1>
    <p></p>
<center><table border="0" cellpadding="0" cellspacing="0" class="graph">
  <tr><td align="center">
  <map id="class_hierarchy_for_interactiv" name="class_hierarchy_for_interactiv">
<area shape="rect" href="console.interactive.InteractiveCommand-class.html" title="InteractiveCommand" alt="" coords="5,5,144,32"/>
<area shape="rect" href="console.interactive.InteractiveShell-class.html" title="InteractiveShell" alt="" coords="168,5,275,32"/>
</map>
  <img src="class_hierarchy_for_interactiv.gif" alt='Class Hierarchy for InteractiveCommand and InteractiveShell' usemap="#class_hierarchy_for_interactiv" ismap="ismap" class="graph-with-title" />
  </td></tr>
  <tr><td align='center'>
<span class="graph-title">Class Hierarchy for InteractiveCommand and InteractiveShell</span>
  </td></tr>
</table><br /></center>    <p></p>

<hr />
<div class="fields">      <p><strong>Author:</strong>
        <a href="mailto:peacmen@tacnetsol.com" target="_top">Peter 
        Eacmen</a>
      </p>
      <p><strong>Organization:</strong>
        Tactical Network Solutions
      </p>
      <p><strong>License:</strong>
        Open Source MIT License
      </p>
      <p><strong>Copyright:</strong>
        &copy; 2010 Tactical Network Solutions
      </p>
      <p><strong>Requires:</strong>
        Python 2.6
      </p>
      <p><strong>Version:</strong>
        1.0
      </p>
      <p><strong>Change Log:</strong>
        <i>interactive.py 2 2010-11-29 13:48:00 -0400 bbengfort</i>
      </p>
      <p><strong>Since:</strong>
        2010-11-29 17:20:39 -0400
      </p>
      <p><strong>Bug:</strong>
        Giving a short-option to the <a 
    href="console.interactive.InteractiveShell-class.html" 
    class="link">InteractiveShell</a> throws an unhandled 
    <code>getopt</code> error
      </p>
      <strong>To Do:</strong>
      <ul class="nomargin-top">
        <li>
        Implement doctests
        </li>
        <li>
        Implement shortopts
        </li>
      </ul>
      <dl><dt>See Also:</dt>
        <dd>
          <a href="http://tacnetsol.com" target="_top">The Tactical Network
          Solutions homepage</a>,
          <a href="http://code.google.com/p/console/" target="_top">Google 
          Code Project</a>,
          <a href="http://code.google.com/p/console/wiki/console" 
          target="_top">The console Wiki</a>
        </dd>
      </dl>
</div><!-- ==================== CLASSES ==================== -->
<a name="section-Classes"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td colspan="2" class="table-header">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr valign="top">
        <td align="left"><span class="table-header">Classes</span></td>
        <td align="right" valign="top"
         ><span class="options">[<a href="#section-Classes"
         class="privatelink" onclick="toggle_private();"
         >hide private</a>]</span></td>
      </tr>
    </table>
  </td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="console.interactive.InteractiveCommand-class.html" class="summary-name">InteractiveCommand</a><br />
      Base class for all commands to be added to the Interactive Shell.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="console.interactive.CustomCompleter-class.html" class="summary-name">CustomCompleter</a><br />
      A custom implementation of global_matches from the rlcompleter module
      that will NOT use __builtin__.__dict__ and python language keywords 
      for completion.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="console.interactive.InteractiveShell-class.html" class="summary-name">InteractiveShell</a><br />
      Gives the user a custom prompt and the ability to type built in 
        commands to the interactive console.
    </td>
  </tr>
</table>
<!-- ==================== FUNCTIONS ==================== -->
<a name="section-Functions"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td colspan="2" class="table-header">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr valign="top">
        <td align="left"><span class="table-header">Functions</span></td>
        <td align="right" valign="top"
         ><span class="options">[<a href="#section-Functions"
         class="privatelink" onclick="toggle_private();"
         >hide private</a>]</span></td>
      </tr>
    </table>
  </td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type"><code>dict</code></span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="console.interactive-module.html#get_option_dict" class="summary-sig-name">get_option_dict</a>(<span class="summary-sig-arg">args</span>,
        <span class="summary-sig-arg">longopts</span>)</span><br />
      Utilizes <code>getopt</code> to create a dictionary of options and 
      values from <code>sys.argv</code>, which should be passed as the 
      first argument to this method.</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="console.interactive-pysrc.html#get_option_dict">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
</table>
<p class="indent-wrapped-lines"><b>Imports:</b>
  <span title="re">re</span>,
  <span title="sys">sys</span>,
  <span title="getopt">getopt</span>,
  <span title="traceback">traceback</span>,
  <span title="readline">readline</span>,
  <span title="rlcompleter">rlcompleter</span>,
  <span title="exceptions.NotImplementedError">NotImplementedError</span>
</p><br />
<!-- ==================== FUNCTION DETAILS ==================== -->
<a name="section-FunctionDetails"></a>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td colspan="2" class="table-header">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr valign="top">
        <td align="left"><span class="table-header">Function Details</span></td>
        <td align="right" valign="top"
         ><span class="options">[<a href="#section-FunctionDetails"
         class="privatelink" onclick="toggle_private();"
         >hide private</a>]</span></td>
      </tr>
    </table>
  </td>
</tr>
</table>
<a name="get_option_dict"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">get_option_dict</span>(<span class="sig-arg">args</span>,
        <span class="sig-arg">longopts</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="console.interactive-pysrc.html#get_option_dict">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <p>Utilizes <code>getopt</code> to create a dictionary of options and 
  values from <code>sys.argv</code>, which should be passed as the first 
  argument to this method. The second argument, longopts should be the long
  name of the option, appended with an '=' to show that a value is 
  expected.</p>
  <dl class="fields">
    <dt>Parameters:</dt>
    <dd><ul class="nomargin-top">
        <li><strong class="pname"><code>args</code></strong> (<code>str</code>) - Arguments string to be parsed, usually <code>sys.argv[1:]</code></li>
        <li><strong class="pname"><code>longopts</code></strong> (<code>list</code> of <code>str</code>) - List of option names in longform, including the '=' at the end</li>
    </ul></dd>
    <dt>Returns: <code>dict</code></dt>
        <dd>Dictionary of options as keys and parsed <code>sys.argv</code> as
          values</dd>
  </dl>
<div class="fields">      <p><strong>Note:</strong>
        shortopts are not yet implemented
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span>fooargs = [<span class="py-string">'foo.py'</span>, <span class="py-string">'--option=bar'</span>]
<span class="py-prompt">&gt;&gt;&gt; </span>opts = get_option_dict(fooargs[1:], [<span class="py-string">'option='</span>,])
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">if</span> <span class="py-string">'option'</span> <span class="py-keyword">in</span> opts:
<span class="py-more">... </span>    <span class="py-keyword">print</span> <span class="py-string">'pass'</span>
<span class="py-more">... </span><span class="py-keyword">else</span>:
<span class="py-more">... </span>    <span class="py-keyword">print</span> <span class="py-string">'fail'</span>
<span class="py-more">... </span>
<span class="py-output">pass</span></pre>
      </p>
</div></td></tr></table>
</div>
<br />
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="console-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="http://code.google.com/p/console/">console</a></th>
          </tr></table></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Tue Nov 30 11:33:30 2010
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>
